package com.six.controller;

import cn.hutool.core.bean.BeanUtil;
import com.six.domain.Cart;
import com.six.dto.SaveCartDTO;
import com.six.service.UserService;
import com.six.vo.cartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

//用户控制层，对购物车进行增删改查
@RestController
@RequestMapping("/user")
public class UserController {
//    注入service
    @Autowired
    private UserService userService;

    //    写对购物车增删改查的四个方法
//    添加或更新
    @PostMapping("/save")
    public void save(@RequestBody SaveCartDTO dto){
        Cart cart =new Cart();
        BeanUtil.copyProperties(dto,cart);
        userService.saveOrUpdate(cart);
    }
//    根据商品id删除
    @DeleteMapping ("/delete")
    public void delete(@PathVariable("id")int cart_id){
         userService.removeById(cart_id);
    }
//   查所有
    @GetMapping("/All")
    public List<cartVO> findall(){
        List<Cart> list = userService.list();
        List<cartVO> result =BeanUtil.copyToList(list,cartVO.class);
        return result;
    }

}
